﻿using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using BCCommon;
using BCCommon.OAEnums;
using BCDto.MySql.Admin;
using BCDto.OA.Attendance.AttendanceCheckInFlow;
using BCDto.OA.Attendance.AttendanceCheckInRecord;
using BCService.OA.Attendance.AttendanceCheckInFlow;
using BCService.OA.Attendance.AttendanceCheckInRecord;
using BCService.WebDomainService.DepartmentService;

namespace BlackCoffeeRESTful.Controllers.Management.OA.Attendance
{
    /// <summary>
    /// 考勤打卡记录信息
    /// </summary>
    [ApiExplorerSettings(GroupName = "OAAdmin")]
    [Route("api/v1/sim/OA/[controller]")]
    public class AttendanceCheckInRecordController : ManagementBaseController
    {
        #region 私有变量

        private readonly IAttendanceCheckInRecordService attendanceCheckInRecordService;
        private readonly IAttendanceCheckInFlowService attendanceCheckInFlowService;
        private readonly IDepartmentService departmentService;

        #endregion 私有变量

        public AttendanceCheckInRecordController(IAttendanceCheckInRecordService attendanceCheckInRecordService, IAttendanceCheckInFlowService attendanceCheckInFlowService, IDepartmentService departmentService)
        {
            this.attendanceCheckInRecordService = attendanceCheckInRecordService;
            this.attendanceCheckInFlowService = attendanceCheckInFlowService;
            this.departmentService = departmentService;
        }

        /// <summary>
        /// 分页获取员工打卡记录信息列表
        /// </summary>
        /// <param name="adminId">登录的管理员</param>
        /// <param name="pageIndex">分页页码</param>
        /// <param name="pageSize">分页页数</param>
        /// <param name="ascending">是否升序排</param>
        /// <param name="status">打卡状态</param>
        /// <param name="sourceType">来源类型</param>
        /// <param name="checkInType">打卡类型</param>
        /// <param name="startTime">打卡开始时间</param>
        /// <param name="endTime">打卡结束时间</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<AttendanceCheckInRecordDto>), 200)]
        public IActionResult Query([FromQuery(Name = "admin_id")]long? adminId,
            [FromQuery(Name = "ascending")]bool ascending = false,
            [FromQuery(Name = "status")]CheckInStatus? status = null,
            [FromQuery(Name = "source_type")]SourceType? sourceType = null,
            [FromQuery(Name = "check_in_type")]CheckInType? checkInType = null,
            [FromQuery(Name = "start_time")]DateTime? startTime = null,
            [FromQuery(Name = "end_time")]DateTime? endTime = null,
            int pageIndex = 1,
            int pageSize = 10)
        {
            bool isHumanResources = this.Admin.JobTitleKey == "StaffTitle_HRSpecialist_HumanResources"
                                    || this.Admin.JobTitleKey == "StaffTitle_PersonnelOfficer_HumanResources"
                                    || this.Admin.JobTitleKey == "StaffTitle_Director_HumanResources";
            if (this.Admin.IsSystemAccount || isHumanResources)
            {
                return Ok(this.attendanceCheckInRecordService.GetList(new AdminDto { CompanyId = this.Admin.CompanyId, AdminId = 0 }, pageIndex, pageSize, ascending, sourceType, status, checkInType, startTime, endTime));
            }
            else if (departmentService.CheckPrincipal(this.Admin.AdminId))
            {
                return Ok(this.attendanceCheckInRecordService.GetList(new AdminDto { CompanyId = this.Admin.CompanyId, AdminId = 0 }, pageIndex, pageSize, ascending, sourceType, status, checkInType, startTime, endTime, this.Admin.DepartmentId));
            }
            return Ok(this.attendanceCheckInRecordService.GetList(this.Admin, pageIndex, pageSize, ascending, sourceType, status, checkInType, startTime, endTime));
        }

        /// <summary>
        /// 查询打卡记录流水信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("[action]/{check_in_record_id}")]
        [ProducesResponseType(typeof(List<AttendanceCheckInFlowDto>), 200)]
        public IActionResult ViewCheckInFlows([FromRoute(Name = "check_in_record_id")] long checkInRecordId)
        {
            return Ok(attendanceCheckInFlowService.GetFlowEntities(checkInRecordId));
        }
    }
}
